package com.atguigu.gmall.product.rest;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseCategory1;
import com.atguigu.gmall.product.entity.BaseCategory2;
import com.atguigu.gmall.product.entity.BaseCategory3;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.BaseCategory2Service;
import com.atguigu.gmall.product.service.BaseCategory3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lfy
 * @Description 分类相关请求处理器
 * @create 2023-08-14 16:29
 */
@RestController
public class BaseCategoryController {


    @Autowired
    BaseCategory1Service baseCategory1Service;

    @Autowired
    BaseCategory2Service baseCategory2Service;


    @Autowired
    BaseCategory3Service baseCategory3Service;
    /**
     * 获取所有一级分类
     * @return
     */
    @GetMapping("/admin/product/getCategory1")
    public Result getCategory1(){

        List<BaseCategory1> list = baseCategory1Service.list();
        return Result.ok(list);
    }

    /**
     * 获取某个一级分类下的所有二级分类
     * @param c1Id
     * @return
     */
    @GetMapping("/admin/product/getCategory2/{c1Id}")
    public Result getCategory2(@PathVariable("c1Id") Long c1Id){

        //1、QueryWrapper 需要自己new
//        QueryWrapper<BaseCategory2> wrapper = new QueryWrapper<>();
//        wrapper.eq("category1_id",c1Id);
//        List<BaseCategory2> list = baseCategory2Service.list(wrapper);


        //2、lambdaQuery： 硬编码；  得到 lambdaQuery、拼装条件、获取结果
        List<BaseCategory2> list = baseCategory2Service.lambdaQuery()
                .eq(BaseCategory2::getCategory1Id, c1Id)
                .list();

        return Result.ok(list);
    }


    /**
     * 获取某个二级分类下的所有三级分类
     * @param c2id
     * @return
     */
    @GetMapping("/admin/product/getCategory3/{c2id}")
    public Result getCategory3(@PathVariable("c2id") Long c2id){

        List<BaseCategory3> list = baseCategory3Service.lambdaQuery()
                .eq(BaseCategory3::getCategory2Id, c2id)
                .list();

        return Result.ok(list);
    }


}
